package com.fastloan.mgt.product.basics.controller;

import com.fastloan.mgt.dictionary.service.DictionaryService;
import com.fastloan.mgt.log.aspect.SystemLog;
import com.fastloan.mgt.product.basics.model.Product;
import com.fastloan.mgt.product.basics.service.ProductService;
import com.fastloan.safe.core.util.ManagerKit;
import com.fastloan.safe.mgt.auth.model.CmuRole;
import com.fastloan.safe.mgt.auth.service.CmuRoleService;
import com.fastloan.safe.mgt.org.model.vo.OrgTreeVo;
import com.fastloan.safe.mgt.org.service.CmuOrgService;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.naming.ldap.PagedResultsControl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.fastloan.safe.core.param.RoleTypeEnum.MAKE_LOANS;
import static java.util.stream.Collectors.toList;

/**
 * 产品controller
 *
 * @author chenkai
 * createTime 2017-11-2017/11/6 17:22
 */
@RestController
@RequestMapping("/api/web/product")
public class ProductController extends BaseController<Product> {
    @Autowired
    private ProductService productService;

    @Autowired
    private CmuOrgService cmuOrgService;

    @Autowired
    private CmuRoleService cmuRoleService;

    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 创建产品
     *
     * @param product
     * @param file
     * @return
     */
    @PostMapping
    @SystemLog(description = "新建产品")
    public ResultTo createProduct(Product product, MultipartFile[] file) {
        return new ResultTo().setData(productService.createProduct(product, file));
    }

    /**
     * 获取产品信息
     *
     * @param productId
     * @return
     */
    @GetMapping("/{productId}")
    public ResultTo getProductId(@PathVariable String productId) {
        return new ResultTo().setData(productService.getProduct(productId));
    }

    /**
     * 更新产品信息
     *
     * @param product
     * @return
     */
    @PutMapping
    @SystemLog(description = "更新产品信息")
    public ResultTo updateProduct(Product product) {
        return productService.updateByPrimaryKeySelective(product) != 0 ? new ResultTo() : new ResultTo(ResultEnum.OPERATION_FAILED);
    }

    /**
     * 获取产品字典信息
     *
     * @return
     */
    @GetMapping("/dictionary")
    public ResultTo getDictionary() {
        try {
            return new ResultTo().setData(productService.getDictionary());
        }catch (Exception e){
            return new ResultTo(ResultEnum.OPERATION_FAILED, "未登录");
        }
    }

    /**
     * 产品分页
     *
     * @return
     */
    @RequestMapping("/pageList")
    public ResultTo pageList() {
        Map<String, Object> map = new HashMap<>();
        List<OrgTreeVo> orgTreeVos = cmuOrgService.selectSonByTop(ManagerKit.getOrgId(), true, false);
        List<String> orgIds = orgTreeVos.stream().map(OrgTreeVo::getId).collect(toList());
        map.put("orgIds", orgIds);
        return dataPage("productPageList", map);
    }

    /**
     * 产品发布/禁用
     *
     * @param productId
     * @return
     */
    @PutMapping("/{productId}")
    @SystemLog(description = "产品发布/禁用")
    public ResultTo releaseOrForbidden(@PathVariable String productId) {
        try {
            return productService.releaseOrForbidden(productId) != null ? new ResultTo() : new ResultTo(ResultEnum.OPERATION_FAILED);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultTo(ResultEnum.OPERATION_FAILED, e.getMessage());
        }
    }

    /**
     * 产线所有产品
     *
     * @return
     */
    @GetMapping("/all")
    public ResultTo allProduct() {
        return new ResultTo().setData(productService.getAllProduct());
    }


    @GetMapping("/loanRole")
    public ResultTo loanRole() {
        return new ResultTo().setData(cmuRoleService.selectByRoleType(MAKE_LOANS));
    }


    @GetMapping("/verify/{productName}")
    public ResultTo verify(@PathVariable String productName) {
        Product product = new Product();
        product.setProductName(productName);
        product.setOrgId(ManagerKit.getOrgId());
        Product one = productService.selectOne(product);
        if (one != null) {
            return new ResultTo(ResultEnum.OPERATION_FAILED, "产品名称已存在");
        }
        return new ResultTo();
    }
//
//    @GetMapping("/funding")
//    public ResultTo funding (){
//        return new ResultTo().setData(dictionaryService.queryDictionaryVO("funding"));
//    }

}
